<?php
    class Table extends MY_Controller {                
    
        function _init() {
            
        }
        
        function _table_file($table) {
            $tablefile = APPPATH . 'tables/' . $table . EXT;
            if(!file_exists($tablefile)) {
                redirect('admin', 'refresh');
            }                        
            include_once $tablefile;            
        }
        
        function view($table = FALSE) {
            if($table == FALSE) {
                redirect('admin', 'refresh');                
            }
            
            $table_data = $this->db->get($table)->result_array();
            $tablefile = APPPATH . 'tables/' . $table . EXT;
            if(!file_exists($tablefile)) {
                redirect('admin', 'refresh');
            }                        
            include_once $tablefile;
                        
            $cname = $table.'_config';
            
            if(isset($$cname)) {
                $table_config = $$cname;
            }                        
                                    
            $view_data = array(
                'fields' => $table_data,
                'fields_config' => $table_config,
                'action' => 'groupupdate',
                'table' => $table,
                'VIEW' => 'list',                          
            );
            $this->load->vars($view_data);
            $this->load->view('admin/maket');
            //print nl2br(print_r($fields, true));
        }
        
        function edit($table, $id = FALSE) {            
            $action = 'edit';
            $res = $this->db->where('id', $id)->get($table);
            if($res->num_rows() == 0 && $this->input->post($table.'_'.$action.'_submit') == FALSE) {
                redirect('admin/table/view/'.$table, 'refresh');
            }
            $item_data = $res->row_array();                                    
            include_once APPPATH . 'tables/' . $table . EXT;            
            $cname = $table.'_config';
            
            if(isset($$cname)) {
                $table_config = $$cname;
            }
            $uploaderror = '';
            if($this->input->post($table.'_'.$action.'_submit') != FALSE) {
                $upload_field = FALSE;
                $update = array();
                foreach($table_config as $index => $item) {
                    if($this->input->post($index) != FALSE) {
                        $update[$index] = $this->input->post($index); 
                    }                    
                    if($item['type'] == 'checkbox') {                        
                        $update[$index] = ($this->input->post($index) == 1 ? 1 : 0); 
                    }
                    
                    if($item['type'] == 'file' && !empty($_FILES[$index]['name'])) {
                        $upload_field = $index;                        
                    }
                    
                }
                
                if($upload_field != FALSE) {
                                                    
                    $this->load->library('upload');
                    $this->load->config('system');
                    $config = $this->config->item('upload');
                    
                    $config['upload_path'] = dirname(FCPATH) . '/uploads/';
            		$config['allowed_types'] = 'gif|jpg|png';
            		$config['max_size']	= '1000';
            		$config['max_width']  = '1280';
            		$config['max_height']  = '1024';
            		$this->upload->initialize($config);
            		if(!$this->upload->do_upload($upload_field)) {                        
                        $uploaderror = $this->upload->display_errors();
                    } else {
                        $fileinfo = $this->upload->data();
                        $sfile_path = $config['upload_path'].$fileinfo['file_name'];
                        $sfile_url = 'uploads/'.$fileinfo['file_name'];
                        $dfile_path = $config['upload_path'].'thumbs/';
                        $dfile_url = 'uploads/thumbs/'.$fileinfo['raw_name'].'_thumb'.$fileinfo['file_ext'];
                        $config = $this->config->item('resize');                                                
                        $config['source_image'] = $sfile_path;                        
                        $config['new_image'] = $dfile_path.$fileinfo['file_name'];                    
                        $this->load->library('image_lib', $config);
                        $this->image_lib->resize();
                        $update['big_image'] = $sfile_url;
                        $update['small_image'] = $dfile_url;                        
                    }
                }
                
                $update_id = $this->input->post('id');
                $this->db->where('id', $update_id)->update($table, $update);
                redirect('admin/table/view/'.$table, 'refresh');                                                                
            }
                                    
            $view_data = array(
                'field' => $item_data,
                'fields_config' => $table_config,
                'action' => $action,
                'table' => $table, 
                'error' => '',                               
                'VIEW' => 'view',                
            );
            
            $this->load->vars($view_data);
            $this->load->view('admin/maket');
        }
        
        function add($table, $ret = FALSE) {
            $action = 'add';
            
            include_once APPPATH . 'tables/' . $table . EXT;            
            $cname = $table.'_config';
            
            if(isset($$cname)) {
                $table_config = $$cname;
            }
            
            $field = array();
            $field['id'] = 0;
            $uploaderror = '';
            if($this->input->post($table.'_'.$action.'_submit') != FALSE) {
                $insert = array();
                $upload_field = FALSE;
                                
                foreach($table_config as $index => $item) {
                    if($this->input->post($index) != FALSE) {
                        $insert[$index] = $this->input->post($index); 
                    }
                    
                    if($item['type'] == 'checkbox') {                        
                        $update[$index] = ($this->input->post($index) == 1 ? 1 : 0); 
                    }
                    
                    if($item['type'] == 'file' && !empty($_FILES[$index]['name'])) {
                        $upload_field = $index;                        
                    }                                        
                }                                
                if($upload_field != FALSE) {
                                                    
                    $this->load->library('upload');
                    $this->load->config('system');
                    $config = $this->config->item('upload');
                    
                    $config['upload_path'] = dirname(FCPATH) . '/uploads/';
            		$config['allowed_types'] = 'gif|jpg|png';
            		$config['max_size']	= '1000';
            		$config['max_width']  = '1280';
            		$config['max_height']  = '1024';
            		$this->upload->initialize($config);
            		if(!$this->upload->do_upload($upload_field)) {                        
                        $uploaderror = $this->upload->display_errors();
                    } else {
                        $fileinfo = $this->upload->data();
                        $sfile_path = $config['upload_path'].$fileinfo['file_name'];
                        $sfile_url = 'uploads/'.$fileinfo['file_name'];
                        $dfile_path = $config['upload_path'].'thumbs/';
                        $dfile_url = 'uploads/thumbs/'.$fileinfo['raw_name'].'_thumb'.$fileinfo['file_ext'];
                        $config = $this->config->item('resize');                                                
                        $config['source_image'] = $sfile_path;                        
                        $config['new_image'] = $dfile_path.$fileinfo['file_name'];                    
                        $this->load->library('image_lib', $config);
                        $this->image_lib->resize();
                        $insert['big_image'] = $sfile_url;
                        $insert['small_image'] = $dfile_url;                        
                    }
                }
                //var_dump($this->input->post('userfile'));
                
                $this->db->insert($table, $insert);
                redirect('admin/table/view/'.$table, 'refresh');
            }                        
            
            foreach($table_config as $index => $item) {
                $field[$index] = '';
            }
            
            $view_data = array (   
                'field' => $field,             
                'fields_config' => $table_config,
                'action' => $action,
                'table' => $table,  
                'error' => $uploaderror,                              
                'VIEW' => 'view',
            );
                                               
            $this->load->vars($view_data);            
            $this->load->view('admin/maket');            
        }
        
        function delete($table, $id = FALSE) {
            $action = 'delete';
            
            if($id == FALSE) {
                redirect('admin');
            }
            
            $this->db->where('id', $id)->delete($table);
            redirect('admin/table/view/'.$table, 'refresh');                                                
        }
        
        function groupupdate($table) {
            $action = 'groupupdate';
            
            include_once APPPATH . 'tables/' . $table . EXT;            
            $cname = $table.'_config';
            
            if(isset($$cname)) {
                $table_config = $$cname;
            }
            if($this->input->post($table.'_'.$action.'_submit') != FALSE) {
                //print nl2br(print_r($this->input->post('title'), TRUE));
                foreach($table_config as $index => $item) {
                    $update = array();
                    if($this->input->post($index) != FALSE) {
                        $data = $this->input->post($index);                        
                        foreach($data as $id => $value) {
                            $update = array(
                                $index => $value,
                            );
                            $this->db->where('id', $id)->update($table, $update);
                        }                        
                    }
                }
            }   
            redirect('admin/table/view/'.$table, 'refresh');         
        }    
             
    }
?>